﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Xml.Linq;
using System.Configuration;
using System.IO;
using System.Collections;
using System.Web;
using System.ServiceModel.Activation;
using System.Text.RegularExpressions;



// NOTE: If you change the class name "simpleWcf" here, you must also update the reference to "simpleWcf" in Web.config.

public class simpleWcf : IsimpleWcf
{

    ModuleManager modMgr;
    private string _reportPath;    
    XDocument xmlDoc;
    public ModuleManager ParseXmlObject(string strRptPath)
    {
        ModuleManager _mdleMngr = new ModuleManager();
        _mdleMngr.ModuleItemCollection = new List<ModuleItem>();
        _mdleMngr.SrcFileItemCollection = new List<SourceFileItem>();
        _mdleMngr.CoveredClsCount = 0;
        _mdleMngr.classCoveragePercentage = 0;
        int _ttleClassCount = 0;
        int _ttlMethods = 0;
        int _ttlLinescovered = 0;
        int _ttlNCLines = 0;
        //string strXMLPath = ConfigurationManager.AppSettings["XMLPath"].ToString();
        XDocument xmlDoc = XDocument.Load(strRptPath);
        ///get file time creation
        if (File.Exists(strRptPath))
        {
            _mdleMngr.FileCreationTime = File.GetCreationTime(strRptPath);
        }


        IEnumerable<XElement> modules = xmlDoc.Root.Elements("Module");
        try
        {
            foreach (XElement module in modules)
            {
                ModuleItem _mdleItem = new ModuleItem();
                _mdleItem.NameSpaceItemCollection = new List<NameSpaceItem>();
                _mdleItem.ModuleName = module.Element("ModuleName").Value;
                _mdleItem.LinesCovered = Convert.ToInt32(module.Element("LinesCovered").Value);
                _mdleItem.LinesNotCovered = Convert.ToInt32(module.Element("LinesNotCovered").Value);
                _mdleItem.LinesPartiallycovered = Convert.ToInt32(module.Element("LinesPartiallyCovered").Value);
                _mdleItem.BlocksCovered = Convert.ToInt32(module.Element("BlocksCovered").Value);
                _mdleItem.BlocksNotCovered = Convert.ToInt32(module.Element("BlocksNotCovered").Value);

                IEnumerable<XElement> nameSpaces = module.Elements("NamespaceTable");
                foreach (XElement _nmsItem in nameSpaces)
                {
                    NameSpaceItem _nmeSpaceItem = new NameSpaceItem();
                    _nmeSpaceItem.ClassItemCollection = new List<ClassItem>();
                    _nmeSpaceItem.NameSpaceName = _nmsItem.Element("NamespaceName").Value;
                    _nmeSpaceItem.LinesCovered = Convert.ToInt32(_nmsItem.Element("LinesCovered").Value);
                    _nmeSpaceItem.LinedPartiallyCovered = Convert.ToInt32(_nmsItem.Element("LinesPartiallyCovered").Value);
                    _nmeSpaceItem.LinesNotCovered = Convert.ToInt32(_nmsItem.Element("LinesNotCovered").Value);
                    _nmeSpaceItem.BlocksCovered = Convert.ToInt32(_nmsItem.Element("BlocksCovered").Value);
                    _nmeSpaceItem.BlocksNotCovered = Convert.ToInt32(_nmsItem.Element("BlocksNotCovered").Value);


                    Double totalLines = _nmeSpaceItem.LinesCovered + _nmeSpaceItem.LinesNotCovered + _nmeSpaceItem.LinedPartiallyCovered;
                    //int covPercentage = (clsItem.LinesCovered / 100) * totalLines;
                    Double covPercentage = (Convert.ToDouble(_nmeSpaceItem.LinesCovered) / Convert.ToDouble(totalLines)) * 100;
                    _nmeSpaceItem.NamespaceCovPercentage = covPercentage;
                    IEnumerable<XElement> clsCollection = _nmsItem.Elements("Class");
                    foreach (XElement _clsitem in clsCollection)
                    {
                        ClassItem _classItem = new ClassItem();
                        _classItem.MethodItemCollection = new List<MethodItem>();
                        _classItem.BlocksCovered = Convert.ToInt32(_clsitem.Element("BlocksCovered").Value);
                        _classItem.BlocksNotCovered = Convert.ToInt32(_clsitem.Element("BlocksNotCovered").Value);
                        _classItem.ClassName = _clsitem.Element("ClassName").Value;
                        _classItem.LinesCovered = Convert.ToInt32(_clsitem.Element("LinesCovered").Value);
                        if (_classItem.LinesCovered > 0)
                        {
                            _mdleMngr.CoveredClsCount = _mdleMngr.CoveredClsCount + 1;
                        }
                        else
                        {
                            _mdleMngr.NCoverdClsCount = _mdleMngr.NCoverdClsCount + 1;
                        }
                        _ttleClassCount = _ttleClassCount + 1;
                        _classItem.LinesNotCovered = Convert.ToInt32(_clsitem.Element("LinesNotCovered").Value);
                        _classItem.LinesPartiallyCovered = Convert.ToInt32(_clsitem.Element("LinesPartiallyCovered").Value);

                        IEnumerable<XElement> _mthdcollection = _clsitem.Elements("Method");
                        foreach (XElement _mthdItem in _mthdcollection)
                        {
                            MethodItem _methodItem = new MethodItem();
                            _methodItem.LineItemCollection = new List<LineItem>();
                            _methodItem.BlocksCovered = Convert.ToInt32(_mthdItem.Element("BlocksCovered").Value);
                            _methodItem.BlocksNotCovered = Convert.ToInt32(_mthdItem.Element("BlocksNotCovered").Value);
                            _methodItem.LinedPartiallyCovered = Convert.ToInt32(_mthdItem.Element("LinesPartiallyCovered").Value);
                            _methodItem.LinesCoved = Convert.ToInt32(_mthdItem.Element("LinesCovered").Value);
                            _methodItem.LinesNotCovered = Convert.ToInt32(_mthdItem.Element("LinesNotCovered").Value);
                            _methodItem.MethodName = _mthdItem.Element("MethodName").Value;
                            IEnumerable<XElement> _lineColletion = _mthdItem.Elements("Lines");
                            _ttlMethods = _ttlMethods + 1;
                            foreach (XElement _lneItem in _lineColletion)
                            {
                                LineItem _lineItem = new LineItem();
                                _lineItem.ColEnd = Convert.ToInt32(_lneItem.Element("ColEnd").Value);
                                _lineItem.ColStart = Convert.ToInt32(_lneItem.Element("ColStart").Value);
                                _lineItem.Coverage = Convert.ToInt32(_lneItem.Element("Coverage").Value);
                                _lineItem.LineID = Convert.ToInt32(_lneItem.Element("LineID").Value);
                                _lineItem.LnEnd = Convert.ToInt32(_lneItem.Element("LnEnd").Value);
                                _lineItem.LnStart = Convert.ToInt32(_lneItem.Element("LnStart").Value);
                                _lineItem.SourceFileID = Convert.ToInt32(_lneItem.Element("SourceFileID").Value);
                                _methodItem.LineItemCollection.Add(_lineItem);

                            }
                            _classItem.MethodItemCollection.Add(_methodItem);
                        }
                        _nmeSpaceItem.ClassItemCollection.Add(_classItem);
                    }
                    _mdleItem.NameSpaceItemCollection.Add(_nmeSpaceItem);
                }
                _mdleMngr.ModuleItemCollection.Add(_mdleItem);
            }
        }

        catch (Exception ex)
        {
            throw ex;
        }

        ///to get the total source file count
        int totalFiles = 0;
        IEnumerable<XElement> srcFiles = xmlDoc.Root.Elements("SourceFileNames");
        foreach (XElement elm in srcFiles)
        {
            SourceFileItem srcFileItem = new SourceFileItem();
            srcFileItem.SrcFileId =Convert.ToInt16(elm.Element("SourceFileID").Value);
            srcFileItem.StrFilePath = Convert.ToString(elm.Element("SourceFileName").Value);
            _mdleMngr.SrcFileItemCollection.Add(srcFileItem);
            totalFiles++;

        }

        ///get the Total Lines for the Application level

        foreach (ModuleItem mitem in _mdleMngr.ModuleItemCollection)
        {
            _ttlLinescovered = _ttlLinescovered + mitem.LinesCovered + mitem.LinesPartiallycovered;
            _ttlNCLines = _ttlNCLines + mitem.LinesNotCovered;

        }



        ///Assign the values to the Module Manager
        _mdleMngr.TotalClasses = _ttleClassCount;
        _mdleMngr.TotalFiles = totalFiles;
        _mdleMngr.AvgClsFile = Convert.ToDouble(_ttleClassCount) / Convert.ToDouble(totalFiles);
        _mdleMngr.AvgClsFile = Math.Round(_mdleMngr.AvgClsFile, 2);
        _mdleMngr.Lines = _ttlLinescovered;
        _mdleMngr.NClines = _ttlNCLines;
        _mdleMngr.AvgMethodClass = Convert.ToDouble(_ttlMethods) / Convert.ToDouble(_ttleClassCount);
        //_mdleMngr.AvgMethodClass = Math.Round(_mdleMngr.AvgMethodClass, 2);
        //_mdleMngr.classCoveragePercentage = _mdleMngr.CoveredClsCount / _ttleClassCount;
        _mdleMngr.classCoveragePercentage = (Convert.ToDouble(_mdleMngr.CoveredClsCount) / Convert.ToDouble(_mdleMngr.TotalClasses)) * 100;
        //_mdleMngr.classCoveragePercentage = Math.Round(_mdleMngr.classCoveragePercentage, 2);    
        return _mdleMngr;
    }
    #region IsimpleWcf Members


    public StatisticsReport GetStateReportByModule(string MdleName,string strRptPath)
    {
        int totClsCov = 0;
        int totNClsCov = 0;
        int totClasses = 0;
        int totMethods = 0;
        modMgr = ParseXmlObject(strRptPath);
        StatisticsReport stateRept = new StatisticsReport();
        foreach (ModuleItem mod in modMgr.ModuleItemCollection)
        {
            if (mod.ModuleName == MdleName)
            {
                stateRept.Name = mod.ModuleName;
                stateRept.LOC = mod.LinesCovered;
                stateRept.NCLOC = mod.LinesNotCovered;
                stateRept.PLOC = mod.LinesPartiallycovered;
                stateRept.BlocksCov = mod.BlocksCovered;
                stateRept.BlocksNt = mod.BlocksNotCovered;
                foreach (NameSpaceItem nsItem in mod.NameSpaceItemCollection)
                {
                    if (nsItem.LinesCovered > 0)
                    {
                        foreach (ClassItem clsItem in nsItem.ClassItemCollection)
                        {
                            if (clsItem.LinesCovered > 0)
                            {
                                totClsCov = totClsCov + 1;
                            }
                            else
                            {
                                totNClsCov = totNClsCov + 1;
                            }
                        }
                    }
                    foreach (ClassItem clsItem in nsItem.ClassItemCollection)
                    {

                        totClasses = totClasses + 1;
                        foreach (MethodItem mItem in clsItem.MethodItemCollection)
                        {
                            totMethods = totMethods + 1;
                        }

                    }

                }
            }
        }
        stateRept.TotClsCov = totClsCov;
        stateRept.TotNClsCov = totNClsCov;
        stateRept.AvgMtdClass = Convert.ToDouble(totMethods) / Convert.ToDouble(totClasses);
        return stateRept;
    }

    public StatisticsReport GetStateReportByNamespace(string MdleName, string NmsName,string strRptPath)
    {
        int totClsCov = 0;
        int totNClsCov = 0;
        int totClasses = 0;
        int totMethods = 0;
        modMgr = ParseXmlObject(strRptPath);
        StatisticsReport stateRept = new StatisticsReport();
        foreach (ModuleItem mod in modMgr.ModuleItemCollection)
        {
            if (mod.ModuleName == MdleName)
            {
                foreach (NameSpaceItem nmitem in mod.NameSpaceItemCollection)
                {
                    if (nmitem.NameSpaceName == NmsName)
                    {
                        stateRept.Name = nmitem.NameSpaceName;
                        stateRept.LOC = nmitem.LinesCovered;
                        stateRept.NCLOC = nmitem.LinesNotCovered;
                        stateRept.PLOC = nmitem.LinedPartiallyCovered;
                        stateRept.BlocksCov = nmitem.BlocksCovered;
                        stateRept.BlocksNt = nmitem.BlocksNotCovered;
                        foreach (ClassItem clsItem in nmitem.ClassItemCollection)
                        {
                            if (clsItem.LinesCovered > 0)
                            {
                                totClsCov = totClsCov + 1;
                            }
                            else
                            {
                                totNClsCov = totNClsCov + 1;
                            }
                            totClasses = totClasses + 1;
                            foreach (MethodItem mItem in clsItem.MethodItemCollection)
                            {
                                totMethods = totMethods + 1;
                            }
                        }
                    }

                }
                stateRept.TotClsCov = totClsCov;
                stateRept.TotNClsCov = totNClsCov;
                stateRept.AvgMtdClass = Convert.ToDouble(totMethods) / Convert.ToDouble(totClasses);

            }
        }
        return stateRept;
    }

    public StatisticsReport GetStateReportByClass(string MdleName, string NmsName, string clsName, string strRptPath)
    {
        modMgr = ParseXmlObject(strRptPath);
        StatisticsReport stateRept = new StatisticsReport();
        int totLines = 0;
        int totMethods = 0;
        int totClasses = 0;
        foreach (ModuleItem mod in modMgr.ModuleItemCollection)
        {
            if (mod.ModuleName == MdleName)
            {
                foreach (NameSpaceItem nmitem in mod.NameSpaceItemCollection)
                {
                    if (nmitem.NameSpaceName == NmsName)
                    {
                        foreach (ClassItem clsItem in nmitem.ClassItemCollection)
                        {
                            if (clsItem.ClassName == clsName)
                            {
                                if (clsItem.LinesCovered > 0)
                                {
                                    totClasses = totClasses + 1;
                                }
                                if (clsItem.ClassName == clsName)
                                {
                                    stateRept.Name = clsItem.ClassName;
                                    stateRept.LOC = clsItem.LinesCovered;
                                    stateRept.NCLOC = clsItem.LinesNotCovered;
                                    stateRept.PLOC = clsItem.LinesPartiallyCovered;
                                    stateRept.BlocksCov = clsItem.BlocksCovered;
                                    stateRept.BlocksNt = clsItem.BlocksNotCovered;

                                    foreach (MethodItem mItem in clsItem.MethodItemCollection)
                                    {
                                        totMethods = totMethods + 1;
                                        foreach (LineItem lItem in mItem.LineItemCollection)
                                        {
                                            totLines = totLines + 1;
                                        }
                                    }
                                }
                                stateRept.AvgLnsMtd = Convert.ToDouble(totLines) / Convert.ToDouble(totMethods);
                                stateRept.TotClsCov = totClasses;
                            }

                        }
                    }
                }

            }
        }
        return stateRept;
    }

    public IList<ClassInfo> GetClassInfo(string MdleName, string NmsName, string strRptPath)
    {
        modMgr = ParseXmlObject(strRptPath);
        IList<ClassInfo> _clsInfo = new List<ClassInfo>();
        foreach (ModuleItem mod in modMgr.ModuleItemCollection)
        {
            if (mod.ModuleName == MdleName)
            {
                foreach (NameSpaceItem nmitem in mod.NameSpaceItemCollection)
                {
                    if (nmitem.NameSpaceName == NmsName)
                    {
                        foreach (ClassItem clsItem in nmitem.ClassItemCollection)
                        {
                            ClassInfo cinfo = new ClassInfo();
                            cinfo.ModuleName = MdleName;
                            cinfo.NamespaceName = NmsName;
                            cinfo.Classname = clsItem.ClassName;
                            Double totalLines = clsItem.LinesCovered + clsItem.LinesNotCovered + clsItem.LinesPartiallyCovered;
                            //int covPercentage = (clsItem.LinesCovered / 100) * totalLines;
                            Double covPercentage = (Convert.ToDouble(clsItem.LinesCovered) / Convert.ToDouble(totalLines)) * 100;
                            cinfo.CovPercentage = Math.Round(covPercentage, 2);
                            cinfo.TotalLines = totalLines;
                            _clsInfo.Add(cinfo);
                        }
                    }
                }
            }
        }

        return _clsInfo;
    }


    public IList<ClassInfo> GetClassInfoAppLevel(string strRptPath)
    {
        modMgr = ParseXmlObject(strRptPath);
        IList<ClassInfo> _clsInfo = new List<ClassInfo>();
        foreach (ModuleItem mod in modMgr.ModuleItemCollection)
        {

            foreach (NameSpaceItem nmitem in mod.NameSpaceItemCollection)
            {
                foreach (ClassItem clsItem in nmitem.ClassItemCollection)
                {
                    ClassInfo cinfo = new ClassInfo();
                    cinfo.ModuleName = mod.ModuleName;
                    cinfo.NamespaceName = nmitem.NameSpaceName;
                    cinfo.Classname = clsItem.ClassName;
                    Double totalLines = clsItem.LinesCovered + clsItem.LinesNotCovered + clsItem.LinesPartiallyCovered;
                    //int covPercentage = (clsItem.LinesCovered / 100) * totalLines;
                    Double covPercentage = (Convert.ToDouble(clsItem.LinesCovered) / Convert.ToDouble(totalLines)) * 100;
                    cinfo.CovPercentage = Math.Round(covPercentage, 2);
                    cinfo.TotalLines = totalLines;
                    _clsInfo.Add(cinfo);
                }

            }

        }

        return _clsInfo;
    }


    #endregion

    #region IsimpleWcf Members


    public StatisticsReport GetStateReport(string strRptPath)
    {
        modMgr = ParseXmlObject(strRptPath);
        StatisticsReport stateRept = new StatisticsReport();
        int _loc = 0;
        int _ncloc = 0;
        int _ploc = 0;
        int _blkscov = 0;
        int _blksntcov = 0;
        foreach (ModuleItem mod in modMgr.ModuleItemCollection)
        {

            _loc = _loc + mod.LinesCovered;
            _ncloc = _ncloc + mod.LinesNotCovered;
            _ploc = _ploc + mod.LinesPartiallycovered;
            _blkscov = _blkscov + mod.BlocksCovered;
            _blksntcov = _blksntcov + mod.BlocksNotCovered;

        }
        stateRept.Name = "Project";
        stateRept.LOC = _loc;
        stateRept.NCLOC = _ncloc;
        stateRept.PLOC = _ploc;
        stateRept.BlocksCov = _blkscov;
        stateRept.BlocksNt = _blksntcov;

        return stateRept;
    }

    #endregion

    #region IsimpleWcf Members


    public IList<MethodInfo> GetMethodInfo(string MdleName, string NmsName, string clsName, string strRptPath)
    {
        modMgr = ParseXmlObject(strRptPath);
        IList<MethodInfo> _methodInfo = new List<MethodInfo>();
        
        foreach (ModuleItem mod in modMgr.ModuleItemCollection)
        {
            if (mod.ModuleName == MdleName)
            {
                foreach (NameSpaceItem nmitem in mod.NameSpaceItemCollection)
                {
                    if (nmitem.NameSpaceName == NmsName)
                    {
                        foreach (ClassItem clsItem in nmitem.ClassItemCollection)
                        {
                            double clsTotLines = clsItem.LinesCovered + clsItem.LinesNotCovered + clsItem.LinesPartiallyCovered;
                            double clsCovPercent = (Convert.ToDouble(clsItem.LinesCovered) / Convert.ToDouble(clsTotLines)) * 100;
                            if (clsItem.ClassName == clsName)
                            {
                                foreach (MethodItem mitem in clsItem.MethodItemCollection)
                                {
                                    MethodInfo minfo = new MethodInfo();
                                    minfo.Methodname = mitem.MethodName;
                                    Double totalLines = mitem.LinesCoved + mitem.LinesNotCovered + mitem.LinedPartiallyCovered;
                                    //int covPercentage = (clsItem.LinesCovered / 100) * totalLines;
                                    int mthdlvltotLines = mitem.LinesCoved + mitem.LinesNotCovered + mitem.LinedPartiallyCovered;
                                    Double covPercentage = (Convert.ToDouble(mitem.LinesCoved) / Convert.ToDouble(totalLines)) * 100;
                                    minfo.CovPercentage = Math.Round(covPercentage, 2);
                                    minfo.ClsCovPercentage = clsCovPercent;
                                    int count = 0;

                                    XDocument xmlDoc = XDocument.Load(strRptPath);
                                    foreach (LineItem lItem in mitem.LineItemCollection)
                                    {
                                        if (minfo.FileName == null)
                                        {
                                            IEnumerable<XElement> Lines = from SrceFileItem in xmlDoc.Descendants("SourceFileNames")
                                                                          where SrceFileItem.Element("SourceFileID").Value == lItem.SourceFileID.ToString()
                                                                          select SrceFileItem;

                                            foreach (XElement ele in Lines)
                                            {
                                                minfo.FileName = ele.Element("SourceFileName").Value.Substring(ele.Element("SourceFileName").Value.LastIndexOf('\\') + 1);
                                            }
                                        }

                                        count = count + 1;                                   
                                        
                                        //totLines = totLines + 1;
                                        if (count == 1)
                                        {
                                            minfo.LineNumber = lItem.LnStart;
                                        }
                                    }
                                    minfo.TotalLines = mthdlvltotLines;
                                    _methodInfo.Add(minfo);

                                }
                            }

                        }
                    }
                }
            }
        }
        return _methodInfo;
    }

    public IList<NamespaceInfo> GetNamespaceInfoAll(string strRptPath)
    {
        modMgr = ParseXmlObject(strRptPath);
        IList<NamespaceInfo> _nsInfo = new List<NamespaceInfo>();

        foreach (ModuleItem mItem in modMgr.ModuleItemCollection)
        {
            foreach (NameSpaceItem nItem in mItem.NameSpaceItemCollection)
            {
                int totFiles = 0;
                int totClasses = 0;
                int totMethods = 0;

                NamespaceInfo nInfo = new NamespaceInfo();
                ArrayList arrList = new ArrayList();
                nInfo.ModuleName = mItem.ModuleName;
                nInfo.NamespaceName = nItem.NameSpaceName;
                nInfo.CovLines = nItem.LinedPartiallyCovered + nItem.LinesCovered;
                nInfo.NCLines = nItem.LinesNotCovered;
                Double totalLines = nItem.LinesCovered + nItem.LinesNotCovered + nItem.LinedPartiallyCovered;
                double nsCovPercent = (Convert.ToDouble(nItem.LinesCovered) / Convert.ToDouble(totalLines)) * 100;
                nInfo.NsCovPercentage = nsCovPercent;

                foreach (ClassItem clsItem in nItem.ClassItemCollection)
                {
                    totClasses = totClasses + 1;
                    foreach (MethodItem mdItem in clsItem.MethodItemCollection)
                    {
                        totMethods = totMethods + 1;
                        foreach (LineItem lItem in mdItem.LineItemCollection)
                        {
                            if (!arrList.Contains(lItem.SourceFileID))
                            {
                                arrList.Add(lItem.SourceFileID);
                            }
                        }
                    }
                }

                totFiles = arrList.Count;
                nInfo.TotFiles = totFiles;
                nInfo.TotClasses = totClasses;

                Double AvgClsFile = Convert.ToDouble(totClasses) / Convert.ToDouble(totFiles);
                Double AvgMdsCls = Convert.ToDouble(totMethods) / Convert.ToDouble(totClasses);
                nInfo.AvgMtdClass = AvgMdsCls;
                nInfo.AvgClsFile = AvgClsFile;
                _nsInfo.Add(nInfo);

            }
        }
        return _nsInfo;
    }

    public IList<NamespaceInfo> GetNamespaceInfoAllByModule(string ModName, string strRptPath)
    {
        modMgr = ParseXmlObject(strRptPath);
        IList<NamespaceInfo> _nsInfo = new List<NamespaceInfo>();

        int totFiles = 0;
        int totClasses = 0;
        int totMethods = 0;

        foreach (ModuleItem mItem in modMgr.ModuleItemCollection)
        {
            if (ModName == mItem.ModuleName)
            {
                foreach (NameSpaceItem nItem in mItem.NameSpaceItemCollection)
                {

                    NamespaceInfo nInfo = new NamespaceInfo();
                    ArrayList arrList = new ArrayList();
                    nInfo.ModuleName = mItem.ModuleName;
                    nInfo.NamespaceName = nItem.NameSpaceName;
                    nInfo.CovLines = nItem.LinedPartiallyCovered + nItem.LinesCovered;
                    nInfo.NCLines = nItem.LinesNotCovered;
                    //Double totalLines = nItem.LinesCovered + nItem.LinesNotCovered + nItem.LinedPartiallyCovered;
                    //double nsCovPercent = (Convert.ToDouble(nItem.LinesCovered) / Convert.ToDouble(totalLines)) * 100;
                    nInfo.NsCovPercentage = nItem.NamespaceCovPercentage;

                    foreach (ClassItem clsItem in nItem.ClassItemCollection)
                    {
                        totClasses = totClasses + 1;
                        foreach (MethodItem mdItem in clsItem.MethodItemCollection)
                        {
                            totMethods = totMethods + 1;
                            foreach (LineItem lItem in mdItem.LineItemCollection)
                            {
                                if (!arrList.Contains(lItem.SourceFileID))
                                {
                                    arrList.Add(lItem.SourceFileID);
                                }
                            }
                        }
                    }

                    totFiles = arrList.Count;
                    nInfo.TotFiles = totFiles;
                    nInfo.TotClasses = totClasses;

                    Double AvgClsFile = Convert.ToDouble(totClasses) / Convert.ToDouble(totFiles);
                    Double AvgMdsCls = Convert.ToDouble(totMethods) / Convert.ToDouble(totClasses);
                    nInfo.AvgMtdClass = AvgMdsCls;
                    nInfo.AvgClsFile = AvgClsFile;
                    _nsInfo.Add(nInfo);

                }
            }
        }
        return _nsInfo;
    }


    public IList<ModNamespaceInfoAggr> GetModLevelNamespaceAggReport(string ModName, string strRptPath)
    {
        modMgr = ParseXmlObject(strRptPath);
        IList<ModNamespaceInfoAggr> _nsInfo = new List<ModNamespaceInfoAggr>();

        int totFiles = 0;
        int totClasses = 0;
        int totMethods = 0;
        ArrayList arrList = null;
        foreach (ModuleItem mItem in modMgr.ModuleItemCollection)
        {
            if (ModName == mItem.ModuleName)
            {
                ModNamespaceInfoAggr nInfo = new ModNamespaceInfoAggr();
                Double totalLines = mItem.LinesCovered + mItem.LinesNotCovered + mItem.LinesPartiallycovered;
                Double covPercentage = (Convert.ToDouble(mItem.LinesCovered) / Convert.ToDouble(totalLines)) * 100;
                nInfo.classCoveragePercentage = covPercentage;
                nInfo.FileCreationTime = modMgr.FileCreationTime;
                nInfo.Lines = mItem.LinesCovered + mItem.LinesPartiallycovered;
                nInfo.NClines = mItem.LinesNotCovered;
                nInfo.ModuleName = mItem.ModuleName;


                foreach (NameSpaceItem nItem in mItem.NameSpaceItemCollection)
                {
                    arrList = new ArrayList();
                    foreach (ClassItem clsItem in nItem.ClassItemCollection)
                    {
                        totClasses = totClasses + 1;
                        foreach (MethodItem mdItem in clsItem.MethodItemCollection)
                        {
                            totMethods = totMethods + 1;
                            foreach (LineItem lItem in mdItem.LineItemCollection)
                            {
                                if (!arrList.Contains(lItem.SourceFileID))
                                {
                                    arrList.Add(lItem.SourceFileID);
                                }
                            }

                        }
                        nInfo.TotalClasses = totClasses;
                    }

                }
                totFiles = arrList.Count;
                nInfo.TotalFiles = totFiles;

                Double AvgClsFile = Convert.ToDouble(totClasses) / Convert.ToDouble(totFiles);
                Double AvgMdsCls = Convert.ToDouble(totMethods) / Convert.ToDouble(totClasses);
                nInfo.AvgMethodClass = AvgMdsCls;
                nInfo.AvgClsFile = AvgClsFile;
                _nsInfo.Add(nInfo);
            }
        }
        return _nsInfo;
    }

    public IList<NamespaceClassInfoAggr> GetNmsLvlClassAggReport(string ModName, string NmsNmae, string strRptPath)
    {
        modMgr = ParseXmlObject(strRptPath);
        IList<NamespaceClassInfoAggr> _nsInfo = new List<NamespaceClassInfoAggr>();
        //int totClasses = 0;
        int totLines = 0;
        int totMethods = 0;
        foreach (ModuleItem mItem in modMgr.ModuleItemCollection)
        {
            if (ModName == mItem.ModuleName)
            {
                foreach (NameSpaceItem nItem in mItem.NameSpaceItemCollection)
                {
                    if (nItem.NameSpaceName == NmsNmae)
                    {
                        NamespaceClassInfoAggr clsInfo = new NamespaceClassInfoAggr();
                        Double totalLines = nItem.LinesCovered + nItem.LinesNotCovered + nItem.LinedPartiallyCovered;
                        Double covPercentage = (Convert.ToDouble(nItem.LinesCovered) / Convert.ToDouble(totalLines)) * 100;
                        clsInfo.CovPercentage = covPercentage;
                        clsInfo.NamespaceName = nItem.NameSpaceName;
                        foreach (ClassItem clsItem in nItem.ClassItemCollection)
                        {
                            //totClasses = totClasses + 1;
                            foreach (MethodItem mdItem in clsItem.MethodItemCollection)
                            {
                                totMethods = totMethods + 1;
                                foreach (LineItem lItem in mdItem.LineItemCollection)
                                {
                                    totLines = totLines + 1;
                                }
                            }
                        }
                        Double AvgLnsMds = Convert.ToDouble(totLines) / Convert.ToDouble(totMethods);
                        clsInfo.AvgLnsMtds = AvgLnsMds;
                        _nsInfo.Add(clsInfo);

                    }

                }

            }
        }

        return _nsInfo;
    }

    public IList<ClassInfo> GetNmsLvlClassReport(string ModName, string NmsNmae, string strRptPath)
    {
        modMgr = ParseXmlObject(strRptPath);
        IList<ClassInfo> clsInfoCollection = new List<ClassInfo>();
        //int totClasses = 0;
        int totLines = 0;
        int totMethods = 0;
        foreach (ModuleItem mItem in modMgr.ModuleItemCollection)
        {
            if (ModName == mItem.ModuleName)
            {
                foreach (NameSpaceItem nItem in mItem.NameSpaceItemCollection)
                {
                    if (nItem.NameSpaceName == NmsNmae)
                    {
                        foreach (ClassItem clsItem in nItem.ClassItemCollection)
                        {
                            ClassInfo clsInfo = new ClassInfo();
                            Double totalLines = clsItem.LinesCovered + clsItem.LinesNotCovered + clsItem.LinesPartiallyCovered;
                            Double covPercentage = (Convert.ToDouble(clsItem.LinesCovered) / Convert.ToDouble(totalLines)) * 100;
                            clsInfo.CovPercentage = covPercentage;
                            clsInfo.ModuleName = mItem.ModuleName;
                            clsInfo.NamespaceName = nItem.NameSpaceName;
                            clsInfo.Classname = clsItem.ClassName;
                            //totClasses = totClasses + 1;
                            foreach (MethodItem mdItem in clsItem.MethodItemCollection)
                            {
                                totMethods = totMethods + 1;
                                foreach (LineItem lItem in mdItem.LineItemCollection)
                                {
                                    totLines = totLines + 1;
                                }
                            }
                            Double AvgLnsMtd = Convert.ToDouble(totLines) / Convert.ToDouble(totMethods);
                            clsInfo.AvgLnsMtd = AvgLnsMtd;
                            clsInfoCollection.Add(clsInfo);
                        }

                    }

                }

            }
        }

        return clsInfoCollection;
    }

    public IList<ClassMethodInfoAggr> GetClsLvlMethodAggReport(string modName, string nsName, string clsName, string strRptPath)
    {
        modMgr = ParseXmlObject(strRptPath);
        IList<ClassMethodInfoAggr> _clsMtdCovInfoColln = new List<ClassMethodInfoAggr>();
        int totLines = 0;
        foreach (ModuleItem mod in modMgr.ModuleItemCollection)
        {
            if (mod.ModuleName == modName)
            {
                foreach (NameSpaceItem nmitem in mod.NameSpaceItemCollection)
                {
                    if (nmitem.NameSpaceName == nsName)
                    {
                        ClassMethodInfoAggr clsMtdInfo = new ClassMethodInfoAggr();
                        foreach (ClassItem clsItem in nmitem.ClassItemCollection)
                        {

                            if (clsItem.ClassName == clsName)
                            {

                                double clsTotLines = clsItem.LinesCovered + clsItem.LinesNotCovered + clsItem.LinesPartiallyCovered;
                                double clsCovPercent = (Convert.ToDouble(clsItem.LinesCovered) / Convert.ToDouble(clsTotLines)) * 100;
                                clsMtdInfo.ClassName = clsItem.ClassName;
                                clsMtdInfo.covPercentage = clsCovPercent;
                                //foreach (MethodItem mitem in clsItem.MethodItemCollection)
                                //{
                                //    foreach (LineItem lItem in mitem.LineItemCollection)
                                //    {
                                //        totLines = totLines + 1;
                                //    }
                                //}
                                totLines = clsItem.LinesCovered + clsItem.LinesNotCovered + clsItem.LinesPartiallyCovered;
                                clsMtdInfo.totLines = totLines;
                            }
                        }
                        _clsMtdCovInfoColln.Add(clsMtdInfo);
                    }
                }
            }
        }
        return _clsMtdCovInfoColln;
    }

    //public String GetNamespaceName()
    //{
    //    return HttpContext.Current.Session["Namespace"].ToString();
    //}

    public IList<ClassInfo> GetClassInfoAppLevelSL(string modulename, string namespacename, string strRptPath)
    {
        IList<ClassInfo> clsitemcollection = new List<ClassInfo>();
        //string strXMLPath = ConfigurationManager.AppSettings["XMLPath"].ToString();
        XDocument xmlDoc = XDocument.Load(strRptPath);

        var classes = from classitem in xmlDoc.Descendants("Class")
                      select new
                      {
                          className = classitem.Element("ClassName").Value,
                          //totalLines = Convert.ToInt32(classitem.Element("LinesCovered")) + Convert.ToInt32( classitem.Element("LinesNotCovered")) +Convert.ToInt32(  classitem.Element("LinesPartiallyCovered")),
                          linesCovered = classitem.Element("LinesCovered").Value,
                          linesnotcovered = classitem.Element("LinesNotCovered").Value,
                          linespartiallycovered = classitem.Element("LinesPartiallyCovered").Value,
                          namespaceName = classitem.Parent,
                          moduleName = classitem.Parent.Parent
                      };


        foreach (var clas in classes)
        {
            ClassInfo clsinfo = new ClassInfo();

            clsinfo.Classname = clas.className;
            double totalLines = Convert.ToDouble(clas.linesCovered) + Convert.ToDouble(clas.linesnotcovered) + Convert.ToDouble(clas.linespartiallycovered);
            Double covPercentage = Math.Round((Convert.ToDouble(clas.linesCovered) / totalLines) * 100, 2);
            clsinfo.CovPercentage = covPercentage;
            clsinfo.NamespaceName = clas.namespaceName.Element("NamespaceName").Value;
            clsinfo.ModuleName = clas.moduleName.Element("ModuleName").Value;
            clsinfo.TotalLines = totalLines;

            

            if (namespacename != string.Empty)
            {
                if (clsinfo.NamespaceName == namespacename)
                {
                    if (clsinfo.ModuleName == modulename)
                    {
                        clsitemcollection.Add(clsinfo);
                    }
 
                }
            }

            else if (modulename != string.Empty)
            {
                if (clsinfo.ModuleName == modulename)
                {
                    clsitemcollection.Add(clsinfo);
                }

            }

            else
            {
                clsitemcollection.Add(clsinfo);
 
            }
        }

        return clsitemcollection;
    }

    public IList<SourceCodeInfo> GetRawSourceCode(string modName, string nsName, string clsName, string strRptPath)
    {        
        StreamReader objReader;
        //Hashtable htSrcCode = null; 
        modMgr = ParseXmlObject(strRptPath);
        IList<SourceCodeInfo> srcInfoCollection = new List<SourceCodeInfo>();         
        int srcId = 0;
        string srcPath = string.Empty;
        int srcIdCount = 0;
        foreach (ModuleItem mItem in modMgr.ModuleItemCollection)
        {
            if (modName == mItem.ModuleName)
            {
                foreach (NameSpaceItem nItem in mItem.NameSpaceItemCollection)
                {
                    if (nsName == nItem.NameSpaceName)
                    {
                        foreach (ClassItem clsItem in nItem.ClassItemCollection)
                        {
                            if (clsName == clsItem.ClassName)
                            {
                                foreach (MethodItem mdItem in clsItem.MethodItemCollection)
                                {
                                    foreach (LineItem lItem in mdItem.LineItemCollection)
                                    {
                                        srcIdCount = srcIdCount + 1;
                                        if (srcIdCount == 1)
                                        {
                                            srcId = lItem.SourceFileID;
                                            break;
                                        }
                                    }
                                    break;
                                }
                                foreach (SourceFileItem srcItem in modMgr.SrcFileItemCollection)
                                {
                                    if (srcId == srcItem.SrcFileId)
                                    {
                                        srcPath = srcItem.StrFilePath;
                                    }
                                }
                            }
                        }
                    }
                }
                
            }
        }
        //objReader = File.OpenText(@"C:\MSCoverageReport\CoverageReport\ContentPages\CoverageAggregate.aspx.cs");
        //string content = objReader.ReadToEnd();   
        //Regex regex = new Regex(@"(\r\n|\r|\n)+");
        //content = regex.Replace(content, "<br />");
        string line = string.Empty;
        int lineNumber = 0;
        //objReader = new StreamReader(@"C:\MSCoverageReport\CoverageReport\ContentPages\CoverageAggregate.aspx.cs");
        objReader = new StreamReader(srcPath);
        while ((line = objReader.ReadLine()) != null)
        {
            SourceCodeInfo srcInfo = new SourceCodeInfo();
            lineNumber = lineNumber + 1;
            srcInfo.LineNumber = lineNumber;
            srcInfo.CodeString = line;
            srcInfoCollection.Add(srcInfo);
        }
        objReader.Close();
        return srcInfoCollection;        
    }

    public IList<LineItem> GetLineInfo(string modName, string nsName, string clsName, string strRptPath)
    {
        IList<LineItem> LineInfoCollection = new List<LineItem>();
        modMgr = ParseXmlObject(strRptPath);
        foreach (ModuleItem modItem in modMgr.ModuleItemCollection)
        {
            if (modName == modItem.ModuleName)
            {
                foreach (NameSpaceItem nItem in modItem.NameSpaceItemCollection)
                {
                    if (nsName == nItem.NameSpaceName)
                    {
                        foreach (ClassItem clsItem in nItem.ClassItemCollection)
                        {
                            if (clsName == clsItem.ClassName)
                            {
                                foreach (MethodItem mdItem in clsItem.MethodItemCollection)
                                {
                                    foreach (LineItem lItem in mdItem.LineItemCollection)
                                    {
                                        LineItem lInfo = new LineItem();
                                        lInfo.LnStart = lItem.LnStart;
                                        lInfo.LnEnd = lItem.LnEnd;
                                        lInfo.ColStart = lItem.ColStart;
                                        lInfo.ColEnd = lItem.ColEnd;
                                        lInfo.Coverage = lItem.Coverage; 
                                        LineInfoCollection.Add(lInfo);    
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return LineInfoCollection; 
    }

    #endregion

    public List<FileContainer> GetFileOnfo(string modName, string nsName, string clsName, string strRptPath)
    {
        
        string codePath;
        _reportPath = strRptPath;
       
        List<FileContainer> filecollectioin = new List<FileContainer>();
        modMgr = ParseXmlObject(strRptPath);
        xmlDoc = XDocument.Load(strRptPath);
        var classes = from classitem in xmlDoc.Descendants("Class")
                      where classitem.Parent.Element("NamespaceName").Value == nsName & classitem.Parent.Parent.Element("ModuleName").Value == modName & classitem.Element("ClassName").Value == clsName
                      select classitem;
        var srcFiles = from srcFiles1 in classes.Descendants("SourceFileID")
                       from filename in srcFiles1.Value
                       group srcFiles1 by srcFiles1.Value;



        foreach (var srcfile in srcFiles)
        {
            //var s = from srvFileName in modMgr.SrcFileItemCollection
            //        where srvFileName.SrcFileId == Convert.ToInt32(srcfile.Key)
            //        select srvFileName;

            var s = from srvFileName in xmlDoc.Descendants("SourceFileNames")
                    where srvFileName.Element("SourceFileID").Value == srcfile.Key
                    select srvFileName;
  

            foreach (var schile in s)
            {
                //codePath = schile.StrFilePath;
                codePath = schile.Element("SourceFileName").Value;
                FileContainer fc = new FileContainer();
                fc.SrcCodeInfoCollection = GetCodeContent(codePath,Convert.ToInt32( srcfile.Key));
                char a='\\';
                fc.FileName = codePath;
                filecollectioin.Add(fc);
             
            }
             


        }




        return filecollectioin;

    }

    private List<SourceCodeInfo> GetCodeContent(string path, int sourfileID)
    {
        StreamReader objReader;
         List<SourceCodeInfo> srcInfoCollection=new List<SourceCodeInfo>();
        string line = string.Empty;
                int lineNumber = 0;

         objReader = new StreamReader(path);
                while ((line = objReader.ReadLine()) != null)
                {
                    SourceCodeInfo srcInfo = new SourceCodeInfo();
                    lineNumber = lineNumber + 1;
                    srcInfo.LineNumber = lineNumber;
                    srcInfo.CodeString = line;
                    srcInfo.CovStatus = IslineCovered(lineNumber, sourfileID);
                    srcInfoCollection.Add(srcInfo);
                }
                objReader.Close();
        return srcInfoCollection;

    }

    private string IslineCovered(int lineNUmber, int sourfileID)
    {
        //XDocument xmlDoc = XDocument.Load(_reportPath);
        string IsCovered = null;

        var Lines = from LineItm in xmlDoc.Descendants("Lines")                                      
                    where LineItm.Element("SourceFileID").Value == sourfileID.ToString() & (LineItm.Element("LnStart").Value == lineNUmber.ToString() || LineItm.Element("LnEnd").Value == lineNUmber.ToString())
                    select LineItm;

        if (Lines.Count() == 0)
        {
            IsCovered = "NA";

        }
        foreach (XElement ele in Lines)
        {
            if (ele.Element("Coverage").Value == "0")
            {
                IsCovered = "C";

            }
            else
            {
                IsCovered = "NC";
            }
        }
        return IsCovered;
    }

   
   

 
}